package com.pyue.eam.regi.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import com.aizuda.limiter.annotation.RateLimit;
import com.aizuda.limiter.strategy.IpKeyGenerateStrategy;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pyue.eam.admin.api.constant.ResponseWord;
import com.pyue.eam.admin.api.entity.regi.CqTaskSortEntity;
import com.pyue.eam.common.core.util.R;
import com.pyue.eam.common.excel.annotation.ResponseExcel;
import com.pyue.eam.common.log.annotation.SysLog;
import com.pyue.eam.common.security.util.SecurityUtils;
import com.pyue.eam.regi.service.CqTaskSortService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Sorting
 *
 * @date 2023-11-17 15:34:00
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/cqTaskSort")
@Tag(description = "cqTaskSort", name = "Sorting管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class CqTaskSortController {

    private final CqTaskSortService cqTaskSortService;

    /**
     * 分页查询
     *
     * @param page       分页对象
     * @param cqTaskSort Sorting
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    public R getCqTaskSortPage(@ParameterObject Page page, @ParameterObject CqTaskSortEntity cqTaskSort) {
        LambdaQueryWrapper<CqTaskSortEntity> wrapper = Wrappers.lambdaQuery(cqTaskSort);
        return R.ok(cqTaskSortService.page(page, wrapper));
    }


    /**
     * 通过id查询Sorting
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(cqTaskSortService.getById(id));
    }

    /**
     * 新增Sorting
     *
     * @param cqTaskSort Sorting
     * @return R
     */
    @Operation(summary = "新增Sorting", description = "新增Sorting")
    @SysLog("新增Sorting")
    @PostMapping
    @RateLimit(count = 1, interval = "3s", strategy = {IpKeyGenerateStrategy.TYPE}, message = ResponseWord.WAIT_TIME)
    public R save(@RequestBody CqTaskSortEntity cqTaskSort) {
        cqTaskSort.setUserAvatar(SecurityUtils.getUser().getAvatar());
        return R.ok(cqTaskSortService.save(cqTaskSort));
    }

    /**
     * 修改Sorting
     *
     * @param cqTaskSort Sorting
     * @return R
     */
    @Operation(summary = "修改Sorting", description = "修改Sorting")
    @SysLog("修改Sorting")
    @PutMapping
    public R updateById(@RequestBody CqTaskSortEntity cqTaskSort) {
        return R.ok(cqTaskSortService.updateById(cqTaskSort));
    }

    /**
     * 通过id删除Sorting
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除Sorting", description = "通过id删除Sorting")
    @SysLog("通过id删除Sorting")
    @DeleteMapping
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(cqTaskSortService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     *
     * @param cqTaskSort 查询条件
     * @param ids        导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    public List<CqTaskSortEntity> export(CqTaskSortEntity cqTaskSort, Long[] ids) {
        return cqTaskSortService.list(Wrappers.lambdaQuery(cqTaskSort).in(ArrayUtil.isNotEmpty(ids), CqTaskSortEntity::getId, ids));
    }
}
